﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 3. Write a script that finds the maximal sequence of 
			equal elements in an array. 
				Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} -> {2, 2, 2}. -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		var array = new Array();
		var number = parseInt(prompt("Enter number of elements"));
		if (!isNaN(number)) {
			// Enter the array of numbers
			for (var i = 0, j = 0; i < number; i++) {
				var n = parseInt(prompt("Enter element " + i));
				if (!isNaN(n)) {
					array[j] = n;
					j++;
				}
			}

			jsConsole.writeLine("You have entered: ");
			jsConsole.writeLine(array.join());
			jsConsole.writeLine("");

			// Find sequence
			var equalElement = array[0];
			var equalElementMax = array[0];
			var equalLength = 1;
			var equalLengthMax = 1;

			for (var i = 1; i < array.length; i++) {
				if (array[i] == equalElement)
				{ equalLength++; }
				else
				{
					if (equalLength >= equalLengthMax) {
						equalLengthMax = equalLength;
						equalElementMax = equalElement;
					}

					equalElement = array[i];
					equalLength = 1;
				}
			}

			if (equalLength >= equalLengthMax) {
				equalLengthMax = equalLength;
				equalElementMax = equalElement;
			}

			jsConsole.writeLine("There is maximal sequence of number " + equalElementMax +
								" with length " + equalLengthMax);
		}
		else {
			jsConsole.writeLine("Wrong Input.");
		}

	</script>
</body>
</html>
